package com.yxd.contract;

import com.yxd.connector.http.HttpRequest;
import com.yxd.connector.http.HttpResponse;
import com.yxd.server.Request;
import com.yxd.server.Response;

import java.io.PrintWriter;

/**
 * Created by Administrator on 2016/6/15.
 */
public abstract class HttpProcessor {

    public void handle(HttpRequest request, HttpResponse response) {
        try {
            PrintWriter writer = response.getWriter();
            writer.print(String.format("%s/%s %s %s\r\n", request.getProtocol(), request.getRequstLine().getVersion(), response.getStatus(), "OK"));
            for (String name : response.getHeaderNames()) {
                String value = response.getHeader(name);
                writer.print(String.format("%s:%s\r\n", name, value));
            }
            writer.print("\r\n");
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.processor(request, response);
    }

    protected abstract void processor(HttpRequest request, HttpResponse response);

}
